package com.docker.common.common.widget.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.docker.common.R;

/**
 * 确定样式Dialog
 */
public class ConfirmPriDialog extends CommonBaseDialog {

    private String title;
    private String content;

    public static ConfirmPriDialog newInstance(String title) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        ConfirmPriDialog dialog = new ConfirmPriDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        content = bundle.getString("content");
        title = bundle.getString("title");
    }

    @Override
    public int setUpLayoutId() {
        return R.layout.common_dialog_scroll_confirm;
    }

    @Override
    public void convertView(ViewHolder holder, CommonBaseDialog dialog) {
        holder.setText(R.id.title, title);
//        holder.setText(R.id.message, content);


        TextView content = holder.getView(R.id.message);
        String str = "请您务必审慎阅读，充分理解 服务协议和隐私政策各条款。您可以在设置中查看，删除，变更您的个人信息，并管理您的授权。您可阅读《用户协议》和《隐私协议》如您同意请点击同意开始接受我们的服务";
        SpannableString spannableString = new SpannableString(str);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                ((TextView) view).setHighlightColor(getResources().getColor(android.R.color.transparent));
                if (confimLietener != null) {
                    confimLietener.onPriClick(1);
                }
            }
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ds.linkColor);
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        };

        //第二个参数是开始的下标，第三个参数是结束的下标
        spannableString.setSpan(clickableSpan, 61, 67, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimaryDark)), 61, 67, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                ((TextView) view).setHighlightColor(getResources().getColor(android.R.color.transparent));
                if (confimLietener != null) {
                    confimLietener.onPriClick(2);
                }
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ds.linkColor);
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        };
        spannableString.setSpan(clickableSpan2, 68, 74, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimaryDark)), 68, 74, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        content.setMovementMethod(LinkMovementMethod.getInstance());
        content.setText(spannableString);
        content.setHighlightColor(getResources().getColor(android.R.color.transparent));


        holder.setOnClickListener(R.id.cancel, v -> {
            if (confimLietener != null) {
                confimLietener.onCancle();
            }
        });
        holder.setOnClickListener(R.id.confirm, v -> {
            dismiss();
            if (confimLietener != null) {
                confimLietener.onConfim();
            }
        });
    }

    public interface ConfimLietener {
        void onCancle();

        void onConfim();

        void onPriClick(int flag);
    }

    private ConfimLietener confimLietener;

    public ConfirmPriDialog setConfimLietener(ConfimLietener confimLietener) {
        this.confimLietener = confimLietener;
        return this;
    }


}
